using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Rocuna.Core.Exceptions;

namespace Rocuna.Controls
{
    /// <summary>
    /// Button control for xna.
    /// </summary>
    public abstract class Button : Control
    {
        #region Constrauctor

        /// <summary>
        /// Initializes a new instance of the <see cref="Button"/> class.
        /// </summary>
        public Button(Game game)
            : base(game)
        {
            State = ButtonState.Normal;
            ButtonText = "Button";
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the state.
        /// </summary>
        /// <value>
        /// The state.
        /// </value>
        public ButtonState State { get; set; }

        /// <summary>
        /// Gets or sets the button text.
        /// </summary>
        /// <value>
        /// The button text.
        /// </value>
        public string ButtonText { get; set; }

        #endregion

        #region Methods

        /// <summary>
        /// Draws this instance.
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
            var spriteBatch = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;
            if (spriteBatch == null)
                throw new NotInitializedException();
            spriteBatch.Begin();
            if (Foreground != null)
                spriteBatch.Draw(Foreground, Border.ToRectangle(), Background);
            spriteBatch.End();
        }

        #region Events and handaling

        /// <summary>
        /// Occurs when [click].
        /// </summary>
        public event EventHandler Click;

        /// <summary>
        /// Raises the click event.
        /// </summary>
        protected void RaiseClickEvent()
        {
            if (Click != null)
                Click(this, new EventArgs());
        }

        /// <summary>
        /// Occurs when [double click].
        /// </summary>
        public event EventHandler DoubleClick;

        protected void RaiseDoubleClick()
        {
            if (DoubleClick != null)
                DoubleClick(this, new EventArgs());
        }
        #endregion

        #endregion
    }
}